import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    subjects:[]
  },
  mutations: {
    getAllSubjectsMutations(state,payload){
      state.subjects=payload
    }
  },
  actions: {
    async getAllSubjectsAsync(cxt){
      const {data:{result}}=await Vue.prototype.$api.subjects.getSubjects()
      cxt.commit('getAllSubjectsMutations',result)
    },
    async addSubjectsAsync(cxt,subjects){
       const {code}=await Vue.prototype.$api.subjects.addSubjects(subjects)
       const {data:{result}}=await Vue.prototype.$api.subjects.getSubjects()
       if(code){
         cxt.commit('getAllSubjectsMutations',result)
       }
    }
  },
  getters:{
    showInfo(state){
      return `共有${state.subjects.filter(item=>item.courses.length>0).length}个专业有课程,共有${state.subjects.filter(item=>item.courses.length==0).length}个专业没有课程`
    }
  }
})
